package com.medicine.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.xml.sax.ErrorHandler;

@Configuration
@ConditionalOnProperty(name = "spring.rabbitmq.template.retry.enabled", havingValue = "true")
public class ErrorConfig {

    @Bean
    public DirectExchange exchange(){
        return new  DirectExchange("error.exchange");
    }
    @Bean
    public Queue errorQueue(){
        return new Queue("error.queue");
    }

    @Bean
    public Binding binding(){
        return BindingBuilder.bind(errorQueue()).to(exchange()).with("error.routing.key");
    }

    @Bean
    public MessageRecoverer errorMessageRecoverer(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate, "error.queue", "error.routing.key");
     }
}
